<?php
/**
 * 非常轻量级别的SEditor v0.9
 * 如果需要高级回复 使用时提供 adv_url 就会有高级回复链接
 * 
 * Author: Shaird
 * QQ: 17349620
 * Last Modify: 2009-4-26
 */

class Control_SEditor extends QUI_Control_Abstract{
    function render(){
    	//设置seditor图片路径
    	$img_dir 		=$this->get('base_dir', $this->_context->baseDir() . 'seditor');
    	$id       		= h($this->id()); //获取名称
    	$adv_post_url 	=$this->get('adv_url');
    	$width			=$this->get('width','550px');
    	$height			=$this->get('height','200px');
		$adv_post = $adv_post_url?'<a href="'.$adv_post_url.'">高级回复</a>':'';	
		$smile_dir = $img_dir.'/smile';
        $out = <<<EOT
<script type="text/javascript">
	function doAddTags(tag1,tag2,obj){
		tag1=(tag1=='[Smile]')?'':tag1;
		textarea = document.getElementById(obj);
		//IE
		if (document.selection) {
			textarea.focus();
			var sel = document.selection.createRange();
			if (sel==null) alert(1);
			sel.text = tag1 + sel.text + tag2;
		}else{  //FF
			var len = textarea.value.length;
		    var start = textarea.selectionStart;
			var end = textarea.selectionEnd;
			var scrollTop = textarea.scrollTop;
			var scrollLeft = textarea.scrollLeft;
		    var sel = textarea.value.substring(start, end);
			var rep = tag1 + sel + tag2;
	        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);

			textarea.scrollTop = scrollTop;
			textarea.scrollLeft = scrollLeft;	
		}
	}
	
    function doImage(obj){
		textarea = document.getElementById(obj);
		var url = prompt('请输入图片URL:','http://');
		var scrollTop = textarea.scrollTop;
		var scrollLeft = textarea.scrollLeft;
		if (!url){
    		return false;
		}
		if (document.selection){
			textarea.focus();
			var sel = document.selection.createRange();
			sel.text = '[IMG]' + url + '[/IMG]';
		}else{
			var len = textarea.value.length;
		    var start = textarea.selectionStart;
			var end = textarea.selectionEnd;
			
	        var sel = textarea.value.substring(start, end);
			var rep = '[IMG]' + url + '[/IMG]';
	        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
	
			textarea.scrollTop = scrollTop;
			textarea.scrollLeft = scrollLeft;
		}
	}
	
    function doURL(obj){
	    textarea = document.getElementById(obj);
		var url = prompt('请输入URL:','http://');
		var scrollTop = textarea.scrollTop;
		var scrollLeft = textarea.scrollLeft;
   		if (!url){
    		return false;
		}
		if (document.selection){
			textarea.focus();
			var sel = document.selection.createRange();
			
			if(sel.text==""){
				sel.text = '[URL]'  + url + '[/URL]';
			} else {
				sel.text = '[URL=' + url + ']' + sel.text + '[/URL]';
			}			
		}else{
			var len = textarea.value.length;
		    var start = textarea.selectionStart;
			var end = textarea.selectionEnd;
	        var sel = textarea.value.substring(start, end);
			
			if(sel==""){
				var rep = '[URL]' + url + '[/URL]';
			} else{
				var rep = '[URL=' + url + ']' + sel + '[/URL]';
			}
	        textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
	
			textarea.scrollTop = scrollTop;
			textarea.scrollLeft = scrollLeft;
		}
	}
	
	function doAddColor(tag1,tag2,obj){
    	colordiv = document.getElementById('seditorColor');
    	colordiv.style.display="none";
    	doAddTags(tag1,tag2,obj);
	}
	
	function doAddSmile(smile,obj){
    	doAddTags('[Smile]',smile,obj);
	}
	
	function getSmile(page){
    	var smile_panel='<table cellpadding=0 cellspacing=0>';
    	var i= (page-1)*12+1;
    	//分3行显示  每行4个标签。
    	for (j=1;j<=3;j++){
			smile_panel+='<tr>';
			for (k=1;k<=4;k++){
				var smile_name = i+1000;
				smile_name +='';
    			smile_name = smile_name.substr(1);
				smile_panel +='<td><a href="#" onclick="doAddSmile(\'[S:'+smile_name+']\',\'mytxtarea\'); return false;"><img src="$smile_dir/'+smile_name+'.gif" /></a></td>';
				i++;
			}
			
    		if(j==page){
				smile_panel+='<td class="smilepage currentpage" id="page'+j+'" style="width:15px;"><a href="#" onClick="getSmile('+j+'); return false;">第'+j+'页</a></td>';
			}else{
				smile_panel+='<td class="smilepage" id="page'+j+'" style="width:15px;"><a href="#" onClick="getSmile('+j+'); return false;">第'+j+'页</a></td>';
			}
			smile_panel+='</tr>';
		}
		
		smile_panel+='</table>';
		
		smilediv = document.getElementById('seditorSmile');
		smilediv.innerHTML = smile_panel;
		
		paneldiv = document.getElementById('editor_Smile');
    	abs = getAbsPoint(paneldiv);
    	smilediv.style.left=abs.x+20+'px';
    	smilediv.style.top=abs.y+20+'px';
	}

	function getColor(){
    	colordiv = document.getElementById('seditorColor');
    	color = document.getElementById('editor_Color');
    	abs = getAbsPoint(color);
    	colordiv.style.left=abs.x+20+'px';
    	colordiv.style.top=abs.y+20+'px';
    	colordiv.style.display="block";
	}
	
	function clearAll(){
		smilediv = document.getElementById('seditorSmile');
    	smilediv.innerHTML = "";
    	colordiv = document.getElementById('seditorColor');
    	colordiv.style.display="none";
	}
		
	function getAbsPoint(e){
	　　 var x = e.offsetLeft; 
	　　 var y = e.offsetTop; 
	　　 while(e = e.offsetParent) { 
	　　 x += e.offsetLeft; 
	　　 y += e.offsetTop; 
	　　 } 
	　　 return {"x": x, "y": y}; 
	}
</script>
<style type="text/css">
	#seditorPanel div,form,ul,ol,li,dl,dt,dd,a {margin:0px; padding:0px; overflow:hidden; font-size:12px;}
    #seditorPanel img {border:none;}
	#seditor {width:$width; padding-right:1px; border:1px solid #003399;}
	#seditormenu {height:20px; line-height:20px; background:#EEE; border-bottom:1px solid #DDF;}
	#seditormenu ul {float:left; width:230px; background:url($img_dir/editoricon.gif) left top no-repeat; height:20px; line-height:20px; margin-left:5px;}
	#seditormenu ul li{float:left; width:20px; height:20px; margin-left:3px;}
	#seditormenu ul li a{width:20px; height:20px; display: block;}
	#seditormenu ul li a:hover{border:1px solid #CCC;}
	#seditormenu #advancepost {width:50px; float:right; margin-right:15px; display:inline;}
	#seditormenu #advancepost a {color:#888; height:20px; line-height:20px; text-decoration:none;}
	#seditormenu #advancepost a:hover{text-decoration:underline;}
	#seditor textarea {width:100%; height:$height; overflow-y:auto; border:0;}
	#seditorSmile {position: absolute; text-align:center;}
	#seditorSmile table{border:3px #CCC solid;}
	#seditorSmile table .smilepage{border-top:2px #CCC solid; border-left:2px #CCC solid; background:#F0F0F0;}
	#seditorSmile table a{text-decoration:none; display: block; color:#666;}
	#seditorSmile table a:hover{background:#FFF; display: block;}
	#seditorSmile table #page1{border-top:0px;}
	#seditorSmile table .currentpage{border-left:0px; background:#FFF;}
	#seditorColor {display: none; position: absolute;}
	#seditorColor table{border:3px #CCC solid;}
	#seditorColor table a{border-left:1px #888 solid;border-top:1px #888 solid; border-right:1px #EEE solid; border-bottom:1px #EEE solid; height:12px; width:12px; display:block;}
</style>
<div id="seditorPanel">
	<div id="seditor">
		<div id="seditormenu">
			<ul>
				<li id="editor_B"><a href="#" onClick="doAddTags('[B]','[/B]','mytxtarea'); return false;" title="粗体"></a></li>
				<li id="editor_I"><a href="#" onClick="doAddTags('[I]','[/I]','mytxtarea'); return false;" title="斜体"></a></li>
				<li id="editor_U"><a href="#" onClick="doAddTags('[U]','[/U]','mytxtarea'); return false;" title="下横杠"></a></li>
				<li id="editor_Color"><a href="#" onClick="getColor(); return false;" title="颜色"></a></li>
				<li id="editor_Url"><a href="#" onClick="doURL('mytxtarea'); return false;" title="URL地址"></a></li>
				<li id="editor_Img"><a href="#" onClick="doImage('mytxtarea'); return false;" title="图片"></a></li>
				<li id="editor_Quote"><a href="#" onClick="doAddTags('[QUOTE]','[/QUOTE]','mytxtarea'); return false;" title="引用"></a></li>
				<li id="editor_Smile"><a href="#" onClick="getSmile(1); return false;" title="表情"></a></li>
				<li id="editor_Code"><a href="#" onClick="doAddTags('[CODE]','[/CODE]','mytxtarea'); return false;" title="代码"></a></li>
			</ul>
			<div id="advancepost">$adv_post</div>
		</div>
		<textarea rows="10" id="mytxtarea" name="$id" onFocus="clearAll();"></textarea>
	</div>
	<div id="seditorSmile">
	
	</div>
	<div id="seditorColor">
		<table cellpadding=0 cellspacing=0>
		<tr>
			<td  bgcolor="#000000"><a href="#" onclick="doAddColor('[COLOR:BLACK]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#000088"><a href="#" onclick="doAddColor('[COLOR:000088]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#008800"><a href="#" onclick="doAddColor('[COLOR:008800]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#008888"><a href="#" onclick="doAddColor('[COLOR:008888]','[/COLOR]','mytxtarea'); return false;"></a></td>
		</tr>
		<tr>
			<td  bgcolor="#880000"><a href="#" onclick="doAddColor('[COLOR:880000]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#880088"><a href="#" onclick="doAddColor('[COLOR:880088]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#888800"><a href="#" onclick="doAddColor('[COLOR:888800]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#888888"><a href="#" onclick="doAddColor('[COLOR:888888]','[/COLOR]','mytxtarea'); return false;"></a></td>
		</tr>
		<tr>
			<td  bgcolor="#CCCCCC"><a href="#" onclick="doAddColor('[COLOR:GREY]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#0000FF"><a href="#" onclick="doAddColor('[COLOR:BLUE]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#00FF00"><a href="#" onclick="doAddColor('[COLOR:GREEN]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#00FFFF"><a href="#" onclick="doAddColor('[COLOR:00FFFF]','[/COLOR]','mytxtarea'); return false;"></a></td>
		</tr>
		<tr>
			<td  bgcolor="#FF0000"><a href="#" onclick="doAddColor('[COLOR:RED]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#FF00FF"><a href="#" onclick="doAddColor('[COLOR:FF00FF]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#FFFF00"><a href="#" onclick="doAddColor('[COLOR:YELLOW]','[/COLOR]','mytxtarea'); return false;"></a></td>
			<td  bgcolor="#FFFFFF"><a href="#" onclick="doAddColor('[COLOR:WHITE]','[/COLOR]','mytxtarea'); return false;"></a></td>
		</tr>
		</table>
	</div>
</div>
EOT;

        return $out;
    }
}